package com.teachoa.bean;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class UserTask extends Model<UserTask> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4372306292678382710L;
	
	public static final UserTask dao = new UserTask();
	
	public List<Record> getUsersByPidAndTaskId(String projectId, String taskId) {
		return Db.find("select a.id, a.user_id, b.username from user_task a left join user b on a.user_id = b.id"
				+ " where project_id = ? and task_id = ?", projectId, taskId);
	}

	public List<Record> getUsersByPidAndNode(String projectId, int i) {
		return Db.find("select a.id, a.user_id, b.username from user_task a left join user b on a.user_id = b.id"
				+ " where project_id = ? and node = ?", projectId, i);
	}
	
	public void updateCurrentNodeByPidAndNode(String projectId, int node) {
		String sql2 = "update user_task set is_current_node = 1 where project_id = ? and node = ?";
		String sql = "update user_task set is_current_node = 0 where project_id = ?";
		Db.update(sql, projectId);
		Db.update(sql2, projectId, node);
	}
	
	public List<Record> findWaitTasksByUserId(String userId) {
		String sql = "select a.*, b.name as pname, c.name as nname, d.id as order_id,"
				+ "d.`status` from user_task a "
				+ "left join project b on a.project_id = b.id "
				+ "left join task c on a.task_id = c.id "
				+ "left join `order` d on a.project_id = d.project_id "
				+ "where a.user_id = ? and a.is_current_node = 1 and d.`status` = 1";
		return Db.find(sql, userId);
	}
	
	public boolean checkUserInTask(String userId) {
		String sql = "select count(*) from user_task where user_id = ? and is_current_node = 1";
		List<Record> list = Db.find(sql, userId);
		return list.size()>0;
	}
	
}
